package com.ysg.widget.row;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.InputType;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;

import com.ysg.R;
import com.ysg.enums.InputTypeEnum;
import com.ysg.utils.YStringUtil;

/**
 * 左标签+右文字+箭头
 */
public class YRowItem extends RelativeLayout {
    private TextView tvLabel, tvContent;
    private ImageView ivLabel, ivArrow, ivRight;
    private View lineView;
    private Switch aSwitch;
    private EditText etContent;

    public YRowItem(Context context) {
        super(context);
    }

    public YRowItem(Context context, AttributeSet attrs) {
        super(context, attrs);

        View view = View.inflate(context, R.layout.widget_row_item, this);
        ivLabel = view.findViewById(R.id.ivLabel);
        tvLabel = view.findViewById(R.id.tvLabel);
        etContent = view.findViewById(R.id.etContent);
        tvContent = view.findViewById(R.id.tvContent);
        ivRight = view.findViewById(R.id.ivRight);
        aSwitch = view.findViewById(R.id.aSwitch);
        ivArrow = view.findViewById(R.id.ivArrow);
        lineView = view.findViewById(R.id.lineView);

        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.YRowItem);
        String leftLabel = array.getString(R.styleable.YRowItem_leftLabel);
        String content = array.getString(R.styleable.YRowItem_content);
        String contentTip = array.getString(R.styleable.YRowItem_contentTip);
        int leftIcon = array.getResourceId(R.styleable.YRowItem_leftIcon, 0);
        int rightIcon = array.getResourceId(R.styleable.YRowItem_rightIcon, 0);
        boolean arrowVisible = array.getBoolean(R.styleable.YRowItem_arrowVisible, true);
        boolean switchVisible = array.getBoolean(R.styleable.YRowItem_switchVisible, false);
        boolean switchChecked = array.getBoolean(R.styleable.YRowItem_switchChecked, false);
        boolean lineVisible = array.getBoolean(R.styleable.YRowItem_lineVisible, true);
        boolean editFlag = array.getBoolean(R.styleable.YRowItem_editFlag, false);
        int inputType = array.getInt(R.styleable.YRowItem_inputType, -1);
        String hint = array.getString(R.styleable.YRowItem_hint);
        boolean darkTheme = array.getBoolean(R.styleable.YRowItem_darkTheme, false);
        array.recycle();

        if (0 != leftIcon){
            ivLabel.setImageResource(leftIcon);
            ivLabel.setVisibility(VISIBLE);
        }else{
            ivLabel.setVisibility(GONE);
        }

        if (YStringUtil.isNotEmpty(leftLabel)){
            tvLabel.setVisibility(VISIBLE);
            tvLabel.setText(leftLabel);
        }else{
            tvLabel.setVisibility(GONE);
        }

        if (YStringUtil.isNotEmpty(content)){
            tvContent.setText(content);
            tvContent.setVisibility(VISIBLE);
        }
        if (YStringUtil.isNotEmpty(contentTip)){
            tvContent.setHint(contentTip);
            tvContent.setVisibility(VISIBLE);
        }

        if (0 != rightIcon){
            ivRight.setImageResource(rightIcon);
            ivRight.setVisibility(VISIBLE);
        }else{
            ivRight.setVisibility(GONE);
        }

        if (switchVisible){
            aSwitch.setVisibility(VISIBLE);
            aSwitch.setChecked(switchChecked);
        }else{
            aSwitch.setVisibility(GONE);
        }
        if (editFlag){
            etContent.setVisibility(VISIBLE);
            if (YStringUtil.isNotEmpty(hint)){
                etContent.setHint(hint);
            }
        }else{
            etContent.setVisibility(GONE);
        }
        //输入框类型：密码；手机；金额
        InputTypeEnum inputTypeEnum = InputTypeEnum.getByType(inputType);
        if (inputTypeEnum != null){
            switch (inputTypeEnum){
                case password:
                    etContent.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                    break;
                case phone:
                    etContent.setInputType(InputType.TYPE_CLASS_PHONE);
                    break;
                case money:
                    etContent.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
                    break;
            }
        }

        ivArrow.setVisibility(arrowVisible?View.VISIBLE: View.INVISIBLE);
        lineView.setVisibility(lineVisible?View.VISIBLE: View.GONE);

        if (darkTheme){
            tvLabel.setTextColor(getResources().getColor(R.color.white));
            tvContent.setTextColor(getResources().getColor(R.color.white));
        }

    }

    public Switch getSwitch(){
        return aSwitch;
    }

    public void setContent(String content){
        tvContent.setText(content);
    }

    public void setSwitchStatus(boolean checked){
        aSwitch.setChecked(checked);
    }

    public String getText(){
        return etContent.getText().toString().trim();
    }
    public void setText(String text){
        etContent.setText(text);
    }

    public EditText getEditText(){
        return etContent;
    }

}
